<?php

/**
 * usuario_notas actions.
 *
 * @package    sispatr
 * @subpackage usuario_notas
 * @author     Foxsis
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class usuario_notasActions extends sfActionsFoxsis
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('usuario_notas', 'list');
  }
  
    public function executeList()
	{
        $paginaAtual = $this->_setActualPage();
        $this->filter = $this->_getFilter();
        $listQuery = UsuarioNotasTable::consultarUsuarioNotas($this->filter);
        
        ## cria o paginador
        $this->getContext()->getConfiguration()->loadHelpers('Url');
		$this->pager_layout = new Foxsis_Pager_Layout(
			$pager = new Doctrine_Pager(
				$listQuery,
				$paginaAtual,
				NUM_LIST_ITENS
			),
			new Doctrine_Pager_Range_Sliding(array('chunk' => MAX_PAGER_LINKS)),
			url_for('usuario_notas/list')
		);
		
		## executa a consulta
		$this->usuario_notas_list = $this->pager_layout->execute(array(), Doctrine::HYDRATE_ARRAY);
		
        $this->setTemplate('index');
    }
    
    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE)
		); 

		$this->getUser()->setAttribute(
			sfActionsFoxsis::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);
		
		return $this->redirect($this->getModuleName().'/list');
	}

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new UsuarioNotasForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new UsuarioNotasForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($usuario_notas = Doctrine::getTable('UsuarioNotas')->find(array($request->getParameter('id_usuario_notas'))), sprintf('Object usuario_notas does not exist (%s).', $request->getParameter('id_usuario_notas')));
    $this->form = new UsuarioNotasForm($usuario_notas);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($usuario_notas = Doctrine::getTable('UsuarioNotas')->find(array($request->getParameter('id_usuario_notas'))), sprintf('Object usuario_notas does not exist (%s).', $request->getParameter('id_usuario_notas')));
    $this->form = new UsuarioNotasForm($usuario_notas);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($usuario_notas = Doctrine::getTable('UsuarioNotas')->find(array($request->getParameter('id_usuario_notas'))), sprintf('Object usuario_notas does not exist (%s).', $request->getParameter('id_usuario_notas')));
    $usuario_notas->delete();

    $this->redirect('usuario_notas/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('usuario_notas/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $usuario_notas = $form->save();
	  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
      $this->redirect('usuario_notas/edit?id_usuario_notas='.$usuario_notas->getIdUsuarioNotas());
    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
